import { list, edit, del, categories, add } from '@/api/goods/goods.api'
import { Message, MessageBox } from 'element-ui';
export default {
    namespaced: true,
    state() {
        return {
            goodsList: [],  //表格数据
            currentPage: 1,     // 当前页
            pageSize: 5,        // 每页显示条数
            total: 0,           // 总条数
            dialogFormVisible: false,   //弹窗显隐标杆
            editGoodInfo: {},   // 待编辑商品信息
            categories: {},   // 商品分类列表
            goodData: {},     // 添加商品数据
            GoodData: {},  //表格添加数据
            categoriesArr: [1, 2, 3]     // 添加商品数据
        }
    },
    getters: {},
    mutations: {
        update_dialogVisible(state, payload) {
            state.dialogFormVisible = payload
        },
        update_goodsList(state, payload) {
            state.goodsList = [...payload]
        },
        update_currentPage(state, payload) {
            state.currentPage = payload
        },
        update_pageSize(state, payload) {
            state.pageSize = payload
        },
        update_total(state, payload) {
            state.total = payload
        },
        update_edit(state, payload) {
            state.editGoodInfo = Object.assign({}, payload)
        },
        update_categoriesArr(state, payload) {
            state.categoriesArr = payload
        },
        update_goodData(state, payload) {
            state.goodData = payload
        },
        update_addDate(state, payload) {
            state.GoodData = Object.assign({}, payload)
            state.GoodData.price = payload.price.toString()
        },
    },
    actions: {
        // 请求商品列表
        sync_update_goodsList({ state, commit }) {
            let { currentPage, pageSize } = state
            list({ currentPage, pageSize }).then(res => {
                commit('update_goodsList', res.data.data)
                commit('update_total', res.data.total)
            })
        },
        // 请求商品分类列表
        sync_update_categories({ commit }) {
            categories().then(res => {
                commit('update_categoriesArr', res.data.categories)
            })
        },
        //提交编辑商品内容
        sync_edit_good({ state, commit, dispatch }) {
            edit(state.editGoodInfo).then(res => {
                commit('update_dialogVisible', false)
                dispatch('sync_update_goodsList')
                console.log(state.editGoodInfo);

            })
        },
         //提交添加操作
         sync_add_good({state}) {
            add(state.GoodData).then(res => {
                console.log(res);
                
            }).then(() => {
                Message({
                    type: 'success',
                    message: '添加成功!'
                });
            }).catch(() => {
                Message({
                    type: 'info',
                    message: '添加失败!'
                });
            });

        },
        //提交删除操作
        sync_del_good({ dispatch }, id) {
            MessageBox.confirm('此操作将永久删除该商品, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                del({ id }).then(res => {
                    dispatch('sync_update_goodsList')
                })
                Message({
                    type: 'success',
                    message: '删除成功!'
                });
            }).catch(() => {
                Message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
       
    },
    modules: {}
}